//
//  MineDataChangeViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/1/4.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "MineDataChangeViewController.h"
#import "UnLineTextField.h"
#import "UserServer.h"
@interface MineDataChangeViewController ()
@property (nonatomic, strong) UnLineTextField *inputTF;
@property (nonatomic, strong) UserServer *userServer;
@property (nonatomic, assign) ChangeDataType type;
@end

@implementation MineDataChangeViewController
- (instancetype)initWithChangeType:(ChangeDataType)type{
    if (self == [super init]) {
        self.type = type;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
- (void)setupUI{
    self.inputTF = [UnLineTextField new];
    [self.contentView addSubview:self.inputTF];
    [self.inputTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.top.mas_equalTo(AdaptedHeight(30));
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(40));
    }];
    switch (self.type) {
        case ChangeDataUserName:
        {
            self.title = XLocalized(@"名称");
            [self.inputTF setValue:@10 forKey:Limit_NAME];
            self.inputTF.placeholder = XLocalized(@"请输入名称");
        }
            break;
            
        default:
            break;
    }
    
    XThemeFlatButton *sureBtn = [XThemeFlatButton button];
    [sureBtn setTitle:XLocalized(@"确定") forState:UIControlStateNormal];
    [self.contentView addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.inputTF.mas_bottom).offset(AdaptedHeight(30));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(40));
        make.bottom.mas_equalTo(0);
    }];
    kWeakSelf(self);
    [sureBtn addTapBlock:^(UIButton * _Nonnull btn) {
        [weakself.view endEditing:YES];
        if (!weakself.inputTF.text.length) {
            [weakself.view makeToast:weakself.inputTF.placeholder];
            return ;
        }
        switch (weakself.type) {
            case ChangeDataUserName:
            {
                weakself.userServer.action = @"nickname";
                weakself.userServer.value = weakself.inputTF.text;
            }
                break;
                
            default:
                break;
        }
        [MBProgressHUD showLoadTitile:@"" ToView:weakself.view];
        [weakself.userServer saveAccountComplete:^(BOOL success, NSString * _Nonnull msg) {
            [MBProgressHUD hideHUDForView:weakself.view];
            [weakself.view makeToast:msg];
            if (success) {
                kCurUser.nickname = weakself.inputTF.text;
                [kUserManager saveUserInfo];
                [weakself backBtnClicked];
            }
        }];
    }];
}

- (UserServer*)userServer{
    if (!_userServer) {
        _userServer = [UserServer new];
    }
    return _userServer;
}

@end
